package com.example.black.myapplication.canvas;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.RegionIterator;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.black.myapplication.R;

/**
 * <pre>
 *     author : 28476
 *     e-mail : hao.wang@1hai.cn
 *     time   : 2019/01/08/10:10
 *     desc   : 画板练习
 *     version: 1.0
 * </pre>
 */

public class MyCanvasView extends View {

    private Paint paint;
    private float[] points;
    private float[] points1;
    private Rect rect;
    private RectF rectF;
    private RectF rectF1;
    private RectF rectF2;
    private Path path;
    private float[] radio;
    private Region region;
    private Region region1;
    private Rect emptyRect;

    public MyCanvasView(Context context) {
        super(context);
        initConfig();
    }

    public MyCanvasView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initConfig();
    }

    public MyCanvasView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initConfig();
    }

    private void initConfig() {
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);

        points = new float[]{0,0,60,50,160,200,300,300};
        points1 = new float[] {0,0,100,100,200,200,300,300};

        rect = new Rect(50, 50, 100, 200);
        rectF = new RectF(30.4f, 30.4f, 80f, 60f);
        rectF1 = new RectF(40.4f, 50.4f, 80f, 60f);
        rectF2 = new RectF(100, 100, 400, 500);
        path = new Path();
        radio = new float[]{10,10,10,10,10,10,50,60};

        region = new Region(100, 100, 400, 300);
        region1 = new Region();
        emptyRect = new Rect();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //练习1
       // canvas.drawLine(0,160,260,300,paint);
        //练习2
      //  canvas.drawLines(points,paint);
        //canvas.drawLines(points1,20,2,paint); 数组越界
        //练习3
//        canvas.drawRect(rect,paint);
//        canvas.drawRect(rectF,paint);
//        canvas.drawRect(100,100,50,60,paint);
        //练习4
//        paint.setColor(getResources().getColor(R.color.colorPrimary));
//        canvas.drawRoundRect(rectF,20,20,paint);
        //练习5
//        paint.setColor(getResources().getColor(R.color.colorPrimaryBrownDark));
//        canvas.drawCircle(190,95,25,paint);
//        canvas.drawOval(rectF1,paint);

        //练习6
//        paint.setColor(getResources().getColor(R.color.colorPrimaryPink));
//        canvas.drawArc(
//                rectF2
//                ,-90
//                ,120
//                ,true//false纯弧线，true 闭合的边
//                ,paint
//        );
        //练习7
//        path.moveTo(60,600);//移动起笔位置
//        path.lineTo(60,500);//移动路线位置
//        path.cubicTo(250, 200, 350, 300, 450, 400);//添加三阶贝塞尔曲线
//        path.close();
//        canvas.drawPath(path,paint);

        path.reset();
        //练习8 todo 不理解此部分内容
//        paint.setColor(getResources().getColor(R.color.colorPrimaryTealDark));
//        path.addRoundRect(rectF2,radio,Path.Direction.CCW);
//        path.addArc(rectF2,0,40);
//        canvas.drawPath(path,paint);

        //练习9
        paint.setColor(getResources().getColor(R.color.md_red_700));
        path.reset();
        path.addOval(rectF2,Path.Direction.CCW);
        region1.setPath(path,region);
        RegionIterator iterator = new RegionIterator(region1);
        while (iterator.next(emptyRect)){
            paint.setColor(getResources().getColor(R.color.md_amber_400));
            canvas.drawRect(emptyRect,paint);
        }
    }
}
